package com.kj.xuyuan.adapter;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kj.xuyuan.R;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.DynamicBean;
import com.kj.xuyuan.util.DisplayUtil;
import com.kj.xuyuan.util.GlideUtils;
import com.kj.xuyuan.util.StringUtils;
import com.kj.xuyuan.widget.FullyGridLayoutManager;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

public class DynamicAdapter extends BaseQuickAdapter<DynamicBean, BaseViewHolder> {

    public DynamicAdapter() {
        super(R.layout.item_dynamic);
    }

    @Override
    protected void convert(BaseViewHolder helper, DynamicBean item) {
        helper.setText(R.id.tv_name,item.getUser().getNickname());
        helper.setText(R.id.tv_location,item.getAddress());
        if(item.getTopic() != null && !StringUtils.isEmpty(item.getTopic().getTitle())){
            helper.setVisible(R.id.tv_tag,true);
            helper.setText(R.id.tv_tag,item.getTopic().getTitle());
        }else{
            helper.setVisible(R.id.tv_tag,false);
        }
        helper.setText(R.id.tv_content,item.getContent());
        helper.setText(R.id.tv_comment,item.getComment()+"");
        helper.setText(R.id.tv_time,item.getCreatetime()+item.getView()+"浏览");

        ImageView ivHead = helper.getView(R.id.iv_head);
        GlideUtils.loadCircleImage(mContext, UrlConstants.Base_Http+item.getUser().getAvatar(),ivHead);

        TextView tvSex = helper.getView(R.id.tv_sex);
        tvSex.setText(item.getUser().getYears());
        Drawable sexDrawable = null;
        if(item.getUser().getGender().equals("1")){//1男2女
            tvSex.setBackgroundResource(R.drawable.shape_blue_20);
            sexDrawable = mContext.getResources().getDrawable(R.mipmap.icon_boy);
        }else{
            tvSex.setBackgroundResource(R.drawable.shape_pink_corners_30);
            sexDrawable = mContext.getResources().getDrawable(R.mipmap.icon_girl);
        }
        sexDrawable.setBounds(0,0,sexDrawable.getMinimumWidth(),sexDrawable.getMinimumHeight());
        tvSex.setCompoundDrawables(sexDrawable,null,null,null);

        TextView tvLike = helper.getView(R.id.tv_like_or_chat);
        Drawable likeDrawable = null;
        if(item.getIs_like_message() == 0){//是否点过喜欢 0否 1是
            tvLike.setText("喜欢");
            likeDrawable = mContext.getResources().getDrawable(R.mipmap.icon_like);
        }else{
            tvLike.setText("去聊天");
            likeDrawable = mContext.getResources().getDrawable(R.mipmap.icon_msg);
        }
        likeDrawable.setBounds(0,0,likeDrawable.getMinimumWidth(),likeDrawable.getMinimumHeight());
        tvLike.setCompoundDrawables(likeDrawable,null,null,null);

        TextView tvNice = helper.getView(R.id.tv_like);
        tvNice.setText(item.getLike()+"");
        Drawable niceDrawable = null;
        if(item.getIs_like() == 0){//是否点赞过本条动态 0否 1是
            niceDrawable = mContext.getResources().getDrawable(R.mipmap.icon_nice);
        }else{
            niceDrawable = mContext.getResources().getDrawable(R.mipmap.icon_nice_red);
        }
        niceDrawable.setBounds(0,0,niceDrawable.getMinimumWidth(),niceDrawable.getMinimumHeight());
        tvNice.setCompoundDrawables(niceDrawable,null,null,null);

        RecyclerView rvImage = helper.getView(R.id.rv_image);
        if(StringUtils.isEmpty(item.getImage())){
            rvImage.setVisibility(View.GONE);
        }else{
            DynamicImageAdapter adapter = new DynamicImageAdapter();
            rvImage.setVisibility(View.VISIBLE);
            rvImage.setLayoutManager(new FullyGridLayoutManager(mContext,3));
            if(rvImage.getItemDecorationCount() == 0){
                rvImage.addItemDecoration(new GridSpacingItemDecoration(3, DisplayUtil.dp2px(mContext,5),false));
            }
            rvImage.setAdapter(adapter);
            String[] str = item.getImage().split(",");
            List<String> images = new ArrayList<>();
            for(int i = 0;i<str.length;i++){
                images.add(str[i]);
            }
            adapter.setNewData(images);

            adapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    listener.imageClick(helper.getLayoutPosition(),position);
                }
            });
        }

        helper.addOnClickListener(R.id.tv_like_or_chat);
        helper.addOnClickListener(R.id.tv_like);
        helper.addOnClickListener(R.id.tv_comment);
        helper.addOnClickListener(R.id.tv_tag);
    }

    private ImageClickListener listener;

    public void setImageClickListener(ImageClickListener listener){
        this.listener = listener;
    }

    public interface ImageClickListener{
        void imageClick(int position1,int position2);
    }
}
